package cmcc.charge;

import cmcc.util.*;
import java.sql.*;
public class Charge {
	String code;
	double charge;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public double getCharge() {
		return charge;
	}

	public void setCharge(double charge) {
		this.charge = charge;
	}
	
	public static boolean changeCharge(String charge_code ,String money){
		Connection conn = DB.getConn();
		Statement stmt = DB.getStatement(conn);
		String sql = "update t_charge set charge = '"+money+"' where charge_code = '"+charge_code+"'";
		try {
			stmt.executeUpdate(sql);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	
	public static boolean changeFunc(String fun_id ,String charge_code){
		Connection conn = DB.getConn();
		Statement stmt = DB.getStatement(conn);
		String sql = "update t_charge_rule set charge_code ='"+charge_code+"' where func_id ='"+fun_id+"'";
		try {
			stmt.executeUpdate(sql);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	public static boolean chargeFunc1(String fun_id , String charge_code){
		Connection  conn = DB.getConn();
		Statement stmt = DB.getStatement(conn);
		try{
			String sql1 = "select * from t_charge_rule where func_id ='"+fun_id+"'";
			ResultSet rs = stmt.executeQuery(sql1);
			if(rs.next()){
				String sql2 = "update t_charge_rule set charge_code ='"+charge_code+"' where func_id ='"+fun_id+"'";
				stmt.executeUpdate(sql2);
			}else{
				String sql3 = "insert into t_charge_rule values (?,?)";
				conn.setAutoCommit(false);
				PreparedStatement pstmt = DB.prepare(conn, sql3);
				pstmt.setString(1, fun_id);
				pstmt.setString(2, charge_code);
				pstmt.executeUpdate();
				conn.commit();
				conn.setAutoCommit(true);	
			}
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}		
	}
}
